﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class History : Form
    {
        public static string constr = ConfigurationManager.AppSettings["ConStr"];
        SqlConnection con = new SqlConnection(constr);

        private int memberId;

        private Boolean feesArdyPaid = false;

        private DateTime dueDate;

        private int planDuration = -1;

        Main parent;

        public History()
        {
            InitializeComponent();
        }

        //public history(int memberId)
        //{
        //    InitializeComponent();
        //    this.memberId = memberId;
        //    fetchMemberById(this.memberId);           
        //}

        //public fees(Main ref_parent)
        //{
        //    parent = ref_parent;
        //    InitializeComponent();
        //}

        private void History_Load(object sender, EventArgs e)
        {            
                     
        }

        private void loadGrid()
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter("select id as Id,mid as [Member Id],name as Name,plantype as [Plan Type],dateoftransaction as [Date of Transaction],subcription_period as [Subscription Period],amount as Amount from fees_transaction", con);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dataGridView2.DataSource = dt;
                if (dt.Rows.Count > 0)
                {
                    buttonPrintBill.Enabled = true;
                }
                else
                {
                    DialogResult result = MessageBox.Show("No Transactions found", "Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        this.BeginInvoke(new MethodInvoker(Close));
                    }
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Fess Transaction Load Grid Failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }            
        }

        private void loadCustomGrid(DateTime fromDate, DateTime toDate)
        {
            try
            {
                SqlDataAdapter adapter = new SqlDataAdapter("select id as Id,mid as [Member Id],name as Name,plantype as [Plan Type],dateoftransaction as [Date of Transaction],subcription_period as [Subscription Period],amount as Amount from fees_transaction where dateoftransaction > = CONVERT (date, '" + fromDate.ToString(Constants.DATE_FORMAT) + "', 104)  and dateoftransaction < =  CONVERT (date, '" + toDate.ToString(Constants.DATE_FORMAT) + "', 104)", con);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dataGridView2.DataSource = dt;
                if (dt.Rows.Count > 0)
                {
                    buttonPrintBill.Enabled = true;
                }
                else
                {
                    DialogResult result = MessageBox.Show("No Transactions found", "Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == DialogResult.OK)
                    {
                        this.BeginInvoke(new MethodInvoker(Close));
                    }
                }
            }
            catch (Exception n)
            {
                MessageBox.Show("Fess Transaction Load Custom Grid Failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private int getGridSelectedFeesTransactionId()
        {
            int feesTransactionId = -1;
            int index = dataGridView2.CurrentCell.RowIndex;
            feesTransactionId = int.Parse(dataGridView2.Rows[index].Cells[0].Value.ToString());           
            return feesTransactionId;
        }

        private int getMemberId()
        {
            int feesMemberId = -1;
            int index = dataGridView2.CurrentCell.RowIndex;
            feesMemberId = int.Parse(dataGridView2.Rows[index].Cells[1].Value.ToString());
            return feesMemberId;
        }

        

      
        private void buttonPrintBill_Click(object sender, EventArgs e)
        {
           
        }

        private void History_Load_1(object sender, EventArgs e)
        {
            loadGrid();  
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
           
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
           
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void buttonSearch_Click_1(object sender, EventArgs e)
        {
            DateTime fromDate = DateTime.Parse(dateTimePickerFromDate.Text.ToString());
            DateTime toDate = DateTime.Parse(dateTimePickerToDate.Text.ToString());
            loadCustomGrid(fromDate, toDate);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void buttonPrintBill_Click_1(object sender, EventArgs e)
        {
            bill_for_fees add = new bill_for_fees(getGridSelectedFeesTransactionId(), getMemberId());//this.memberId);
            add.Visible = true;
            add.MdiParent = this.parent;
            add.Show();
        }

        private void buttonCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dateTimePickerFromDate_ValueChanged(object sender, EventArgs e)
        {

        }

       

       
    }
}
